#import "FeedbackViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "L_FeedbackTableViewCell.h"
#import "R_FeedbackTableViewCell.h"
#import "NetWorkManager.h"

@implementation FeedbackViewController

@synthesize mFeedbackDatas = _mFeedbackDatas;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UMFeedback sharedInstance] get];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.backgroundColor =[UIColor clearColor]; //导航上那条黑线的原因在这里
    baseTitle.text = @"反馈";
    
    UIButton* backBtn= [[[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, 44.0, 44.0)] autorelease];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"nav_bar_back_1.png"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"nav_bar_back_2.png"] forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(backToPrevious) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* backBarBtn= [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
    [self.navigationItem setLeftBarButtonItem:backBarBtn];
    
//    self.mTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"messages_tableview_background"]];
    self.mTableView.backgroundColor = [UIColor colorWithRed:235.0/255.0 green:238.0/255.0 blue:240.0/255.0 alpha:1.0];
    
//    [self customizeNavigationBar:self.navigationController.navigationBar];
    _mFeedbackDatas = [[NSArray alloc] init];
    
    [[UMFeedback sharedInstance] setAppkey:UMENG_APPKEY delegate:(id<UMFeedbackDataDelegate>)self];
    
    //    从缓存取topicAndReplies
    self.mFeedbackDatas = [UMFeedback sharedInstance].topicAndReplies;
    [self updateTableView:nil];

    [self baseInitComment:self btnName:@"提交"];
    [self baseOnlyComment];
}

- (void)viewDidUnload
{
    PP_RELEASE(_mTableView);
    PP_RELEASE(_mFeedbackDatas);
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)backToPrevious {
//    [self dismissModalViewControllerAnimated:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)BVCcommentDone:(HPGrowingTextView *)commentFild {
    CGRect commentRect = CGRectMake(6, 3, 250, 40);  //提交反馈后复原输入框的位置
    commentFild.frame = commentRect;
    [commentFild resignFirstResponder];
    if ([commentFild.text length])
    {
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
        [dictionary setObject:commentFild.text forKey:@"content"];
        //        [dictionary setObject:@"2" forKey:@"age_group"];
        //        [dictionary setObject:@"female" forKey:@"gender"];
        
        [[UMFeedback sharedInstance] post:dictionary];
        commentFild.text = @"";
    }
}

- (void)BVCcommentHide:(UIView *)commentView {
    [self baseOnlyComment];
}

#pragma mark tableview delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_mFeedbackDatas count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *content = [[self.mFeedbackDatas objectAtIndex:indexPath.row] objectForKey:@"content"];
    CGSize labelSize = [content sizeWithFont:[UIFont systemFontOfSize:14.0f]
                           constrainedToSize:CGSizeMake(250.0f, MAXFLOAT)
                               lineBreakMode:NSLineBreakByWordWrapping];
    
    
    return labelSize.height + 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *L_CellIdentifier = @"L_UMFBTableViewCell";
    static NSString *R_CellIdentifier = @"R_UMFBTableViewCell";
    
    NSDictionary *data = [self.mFeedbackDatas objectAtIndex:indexPath.row];
    
    if ([[data valueForKey:@"type"] isEqualToString:@"user_reply"])
    {
        R_FeedbackTableViewCell *cell = (R_FeedbackTableViewCell*)[tableView dequeueReusableCellWithIdentifier:R_CellIdentifier];
        if (cell == nil) {
            cell = [[R_FeedbackTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:R_CellIdentifier];
        }
        cell.textLabel.text = [data valueForKey:@"content"];
        return cell;
    }
    else
    {
        L_FeedbackTableViewCell *cell = (L_FeedbackTableViewCell*)[tableView dequeueReusableCellWithIdentifier:L_CellIdentifier];
        if (cell == nil) {
            cell = [[L_FeedbackTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:L_CellIdentifier];
        }
        
        cell.textLabel.text = [data valueForKey:@"content"];
        
        return cell;
    }
}

#pragma mark Umeng Feedback delegate

- (void)updateTableView:(NSError *)error
{
    if ([self.mFeedbackDatas count])
    {
        [self.mTableView reloadData];
        int lastRowNumber = [self.mTableView numberOfRowsInSection:0] - 1;
        NSIndexPath *ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
        [self.mTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
    }
    else
    {
        
    }
}

- (void)updateTextField:(NSError *)error
{
    [[UMFeedback sharedInstance] get];
}

- (void)getFinishedWithError:(NSError *)error
{
    if (!error)
    {
        [self updateTableView:error];
    }
}

- (void)postFinishedWithError:(NSError *)error
{
    HAS_NO_NETWORK(self);
//    UIAlertView *alertView;
    if (!error) {
//        alertView = [[UIAlertView alloc] initWithTitle:@"感谢您的反馈!" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [self baseShowInfoHud:@"感谢您的反馈" andIsTop:NO];
              
    } else{
//        alertView = [[UIAlertView alloc] initWithTitle:@"发送失败!" message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [self baseShowInfoHud:@"发送失败" andIsTop:NO];
    }
//    [alertView show];
    [self updateTextField:error];
}

#pragma mark scrollow delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self baseOnlyComment];
}

- (void)dealloc {
    PP_RELEASE(_mTableView);
    PP_RELEASE(_mFeedbackDatas);
    [super dealloc];
}

@end
